package com.chenggong.demo.activity

import android.os.Bundle
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import com.chenggong.demo.BuildConfig
import com.chenggong.demo.R
import com.chenggong.demo.databinding.ActivityMainBinding
import com.chenggong.demo.permission.PermissionUtils
import com.fengsu.baselib.permission.AuthorityEntity
import com.hd.trans.ui.activity.*
import com.hd.trans.ui.base.TransInit
import com.hd.trans.utils.OpenKotlinAty

class MainActivity : BaseActivity<ActivityMainBinding>(true) {

    private lateinit var adapter: ArrayAdapter<String>
    override val layoutId: Int
        get() = R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {
        //语音翻译SDK初始化
        TransInit.Builder(this)
            .setThemeColor("#1663FF")
            .setAppIconResourceId(R.mipmap.ic_launcher)
            .setIsOpenDomainSwitch(true)
            .setIsOpenHuaweiService(true)
            .setIsOpenOfflineTrans(true)
            .setDebug(BuildConfig.DEBUG)
            .build()


        val data = arrayOf(
            getString(R.string.camera_trans),
            getString(R.string.voice_trans),
            getString(R.string.text_trans),
            getString(R.string.simultaneous_trans),
            getString(R.string.words_trans),
            getString(R.string.web_trans),
            getString(R.string.first_aid_abroad),
            getString(R.string.favorites),
            getString(R.string.offline_trans),
        )
        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
        val listView = findViewById<ListView>(R.id.list)
        listView.adapter = adapter
        listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
            onItemClick(position)
        }

    }

    private fun onItemClick(position: Int) {
        when(adapter.getItem(position)){
            getString(R.string.camera_trans)-> {
                checkPermissions {
                    OpenKotlinAty.openCameraHomeActivity(this, null, null)
                }
            }
            getString(R.string.voice_trans)-> {
                VoiceHomeTwoActivity.open(this,null,null)
            }
            getString(R.string.text_trans)-> {
                TextHomeActivity.open(this,null,null)
            }
            getString(R.string.simultaneous_trans)-> {
                SimulHomeActivity2.open(this,null,null)
            }
            getString(R.string.words_trans)-> {
                checkPermissions {
                    WordTranslateActivity.open(this,null,true)//false:不显示收藏按钮
                }
            }
            getString(R.string.web_trans)-> {
                WebHomeActivity.open(this,null, getString(R.string.web_trans))
            }
            getString(R.string.first_aid_abroad)-> {
                VoiceFirstAidKitActivity.open(this,null)
            }
            getString(R.string.favorites)-> {
                FavoritesActivity.open(this,null,null)
            }
            getString(R.string.offline_trans)-> {
                OfflineHomeActivity.open(this,null)
            }
        }
    }

    private fun checkPermissions(callback:()->Unit){
        PermissionUtils.checkSinglePermission(
            this,
            AuthorityEntity.AuthorityName.CAMERA,
            {
                callback.invoke()
            },
            {
                val authorityEntity = ArrayList<AuthorityEntity>()
                authorityEntity.add(
                    AuthorityEntity(
                        AuthorityEntity.AuthorityName.CAMERA,
                        false,
                        "CAMERA"
                    )
                )
                ShowDialog.showAuthority(this, authorityEntity, open = {
                    PermissionUtils.authPermissions(
                        this,
                        AuthorityEntity.AuthorityName.CAMERA
                    ) {
                        callback.invoke()
                    }
                })
            })
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtils.onRequestPermissionsResult(this, requestCode, permissions, grantResults)
    }
}